var common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    classification: [],
    current: 0,
    items: [],
    onlyWx: false,
    itemloading: true,
    itemIsShow: false,
    page: 1,
    categoryId: '',
    userInfo: app.globalData.userInfo,
    tipshow: true,
    more: true,
    currentTab: 0,
    rToolsShow: false
  },
  onShareAppMessage: function (res) {
    console.log(res)
      if (app.globalData.userInfo.hasShop){
        fromOf = '&fO=fsk'
      }else{
        fromOf = '&fO=fod'
      }
      if(res.target.dataset.item.is99){
        title = '快帮帮我，就差你一个了'
      }else{
        title = app.globalData.userInfo.nickname + '分享给你一个超值特价商品，快来看看吧'
      }
      console.log('/pages/itemdetail/itemdetail?userId=' + app.globalData.userId + '&sku=' + res.target.dataset.item.skuId + fromOf)
      path = '/pages/itemdetail/itemdetail?userId=' + app.globalData.userId + '&sku=' + res.target.dataset.item.skuId + fromOf
      imageUrl = res.target.dataset.item.image
    return {
      title: title,
      path: path,
      imageUrl: imageUrl
    }
  },
  onLoad: function (options) {
    this.getclassificationdata()
    console.log(app.globalData.userInfo)
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },
  getclassificationdata() {
    const that = this
    common.post({
      url: '/app/v2/goods/categorys', data: {}
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          classification: res.data,
          categoryId: res.data[0].id
        });
        wx.setStorage({
          key: "classification",
          data: res.data
        })
        that.getitemdata(res.data[0].id)
      }).catch((res) => {
        console.log(res)
      })

  },
  navChange(e) {
    console.log(e)
    this.setData({
      currentTab: e.currentTarget.dataset.current,
      items: [],
      page: 1,
      categoryId: e.currentTarget.dataset.id,
      itemIsShow: false,
    })
    this.getitemdata(e.currentTarget.dataset.id)
  },
  getitemdata(categoryId) {
    const that = this
    common.post({
      url: '/app/v2/goods/list', data: { categoryId: categoryId, onlyWx: this.data.onlyWx, pageNo: 1,sessionId:app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          items: res.data.data,
          itemIsShow: true
        });
      }).catch((res) => {
        console.log(res)
      })
  },
  getitemdatapage(categoryId) {
    const that = this;
    var page = that.data.page + 1;

    common.post({ url: '/app/v2/goods/list', data: { categoryId: categoryId, pageNo: page,sessionId:app.globalData.sessionId } })
      .then((res) => {
        var data = that.data.items;
        console.log(res.data)
        if (that.data.page * 10 < res.data.total) {
          that.setData({
            page: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            console.log(data)
          }
          // that.setData({
          //   items: data,
          // })
          setTimeout(function () {
            that.setData({
              items: data,
              more: true
            });
            console.log(data)
          }, 300)
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            itemloading: false,
          })
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  getMore() {
    if (this.data.more){
      this.setData({
        itemloading: true,
        more: false
      })
      this.getitemdatapage(this.data.categoryId)
    }
  },
  buybtn(e) {
    if (e.currentTarget.dataset.item.is99) {
      wx.navigateTo({
        url: '/pages/activeitemdetail/activeitemdetail?sku=' + e.currentTarget.dataset.item.skuId
      })
    } else {
      wx.navigateTo({
        url: '/pages/itemdetailNew/itemdetailNew?sku=' + e.currentTarget.dataset.item.skuId
      })
    }
  },
  toMyStone() {
    wx.switchTab({
      url: '/pages/myStore/myStore',
      success: function (res) {
        console.log('toMyStore')
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  closeTip(){
    this.setData({
      tipshow: false
    })
  },
  totop(){
    this.setData({
      scrollTop: 0
    })
  },
  scolly: function (e) {
    // console.log(e.detail.scrollTop)
    if(e.detail.scrollTop > 600){
      this.setData({
        rToolsShow: true
      })
    }else{
      this.setData({
        rToolsShow: false
      })
    }
  },
  seachlink: function () {
    wx.navigateTo({
      url: '/pages/seach/seach'
    })
  },
  jlshare(e){
    console.log(e)
    const that = this
    wx.navigateTo({
      url: '/pages/createSolitaire/createSolitaire',
      events: {
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        console.log('toCreateSolitaire')
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: e.currentTarget.dataset.item })
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
})